package com.example.chapter02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.chapter02.utils.DateUtils;


public class ButtonStyleActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {

    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_style);

        tv_result = findViewById(R.id.tv_result);

        Button btn_single = findViewById(R.id.btn_click_single);
        btn_single.setOnClickListener(new MyOnClickListener(tv_result));

        Button btn_public = findViewById(R.id.btn_click_public);
        btn_public.setOnClickListener(this);


        Button btn_longClick = findViewById(R.id.btn_longClick_public);
        btn_longClick.setOnLongClickListener(this);
        btn_longClick.setLongClickable(false); // 默认为true
        btn_longClick.setOnClickListener(this);

        Button btn_able = findViewById(R.id.btn_able);
        btn_able.setOnClickListener(this);

        Button btn_disable = findViewById(R.id.btn_disable);
        btn_disable.setOnClickListener(this);
    }

    public void doClick(View view) {
        String format = String.format("%s 您点击了按钮 ： %s", DateUtils.getNowTime(), ((Button) view).getText());
        tv_result.setText(format);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_click_public || v.getId() == R.id.btn_longClick_public)
            tv_result.setText(String.format("%s 您单击了按钮 ： %s", DateUtils.getNowTime(), ((Button) v).getText()));
        else if (v.getId() == R.id.btn_able || v.getId() == R.id.btn_disable) {
            Button btn_longClick_public = findViewById(R.id.btn_longClick_public);
            btn_longClick_public.setEnabled(v.getId() == R.id.btn_able);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.btn_longClick_public) {
            tv_result.setText(String.format("%s 您长按了按钮 ： %s", DateUtils.getNowTime(), ((Button) v).getText()));
            return true;
        }
        return false;
    }

    static class MyOnClickListener implements View.OnClickListener {
        private final TextView tv_result;

        public MyOnClickListener(TextView tv_result) {
            this.tv_result = tv_result;
        }

        @Override
        public void onClick(View v) {
            tv_result.setText(
                    String.format(
                            "%s 单机了 %s",
                            DateUtils.getNowTime(),
                            ((Button) v).getText()));
        }
    }
}